package td.pt;

import td.client.ClientState;
import td.client.presenter.RootPresenter;
import td.client.ui.Button;
import td.domain.Category;
import td.domain.DemoStore;
import td.pt.event.ThoughtListingEvent;
import td.pt.event.UseCategoryNavigationEvent;
import td.pt.event.UseTagNavigationEvent;
import td.pt.widget.CategoryTreePopupPresenter;
import td.pt.widget.TopicListingPopupPresenter;
import td.pt.widget.CategoryTreePopupPresenter.CategoryNavigationType;
import td.pt.widget.TagTreePopupPresenter;
import td.pt.widget.TagTreePopupPresenter.TagNavigationType;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ProxyStandard;
import com.gwtplatform.mvp.client.proxy.Proxy;
import com.gwtplatform.mvp.client.proxy.RevealContentEvent;

// TODO: NOTE that GWTP plug-in by default creates the WRONG EventBus class

public class LeftNavigationPresenter
		extends
		Presenter<LeftNavigationPresenter.MyView, LeftNavigationPresenter.MyProxy> {

	public interface MyView extends View {
		public Button getBrowseCategories();

		public Button getBrowseTags();

		public Button getViewTopThoughts();
	}

	@ProxyStandard
	public interface MyProxy extends Proxy<LeftNavigationPresenter> {
	}

	@Inject
	CategoryTreePopupPresenter categoryTreePopupPresenter;
	
	@Inject
	TopicListingPopupPresenter topicListingPopupPresenter;

	@Inject
	TagTreePopupPresenter tagTreePopupPresenter;

	@Inject
	RightNavigationPresenter rightNavPresenter;

	@Inject
	ClientState state;

	@Inject
	public LeftNavigationPresenter(final EventBus eventBus, final MyView view,
			final MyProxy proxy) {
		super(eventBus, view, proxy);
	}

	@Override
	protected void revealInParent() {
		RevealContentEvent.fire(this, RootPresenter.SLOT_leftNavigation, this);
	}

	@Override
	protected void onBind() {
		super.onBind();
		this.getView().getBrowseCategories()
				.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						hideEditControls();
						UseCategoryNavigationEvent searchCategoriesEvent = new UseCategoryNavigationEvent(
								CategoryNavigationType.SELECT_THOUGHTS_IN_CATEGORY,
								LeftNavigationPresenter.this);
						getEventBus().fireEvent(searchCategoriesEvent);
					}
				});
		this.getView().getBrowseTags().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hideEditControls();
				UseTagNavigationEvent searchTagsEvent = new UseTagNavigationEvent(
						TagNavigationType.SELECT_TAGGED_BY_THOUGHTS,
						LeftNavigationPresenter.this);
				getEventBus().fireEvent(searchTagsEvent);
			}
		});
		this.getView().getViewTopThoughts().addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hideEditControls();
				ThoughtListingEvent<Category> newCategoryListingEvent = new ThoughtListingEvent<Category>(
						"Top Thoughts across all Categories",
						DemoStore.db().rootCategory);
				getEventBus().fireEvent(newCategoryListingEvent);
			}
		});
	}

	public void hideEditControls() {
		rightNavPresenter.getView().getAddAReference().setVisible(false);
		rightNavPresenter.getView().getCategorize().setVisible(false);
		rightNavPresenter.getView().getPublish().setVisible(false);
		rightNavPresenter.getView().getAddToActiveReferences()
				.setVisible(false);
		rightNavPresenter.getView().getTag().setVisible(false);
		if (state.getThinker() != null) {
			rightNavPresenter.getView().getCreateThought().setVisible(true);
		}
	}

	@Override
	protected void onReset() {
		super.onReset();
	}
}
